#include<bits/stdc++.h>

using namespace std;
struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode() : val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
	int deepestLeavesSum(TreeNode* root) {
		queue<TreeNode*> q;
		q.push(root);

		int sum = 0;
		while (!q.empty())
		{
			int size = q.size();
			sum = 0;
			for (int i = 0; i < size; i++)
			{
				TreeNode* pop = q.front();
				sum += pop->val;
				q.pop();
				if (pop->left)
				{
					q.push(pop->left);
				}
				if (pop->right)
				{
					q.push(pop->right);
				}
			}
		}

		return sum;
	}
};